package net.entelijan.cobean.examples.showcase.bind.combobox;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;

import javax.swing.JOptionPane;

import net.entelijan.cobean.bind.impl.BoundPropBinding;
import net.entelijan.cobean.combobox.DefaultComboBoxInitializer;
import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.ISingleSelectionListCobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.scf.IShowcase;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ComboBoxShowcase implements IShowcase<ComboBoxPanel> {

	private ILitService Service;
	private ComboBoxShowcaseModel model = new ComboBoxShowcaseModel();

	private static Log log = LogFactory.getLog(ComboBoxShowcase.class);

	public ComboBoxShowcase() {
		super();
	}

	@Override
	public void init(final ComboBoxPanel view) {

		{
			// Create the component bean (cobean)
			DefaultComboBoxInitializer<LitBook> cbi = new DefaultComboBoxInitializer<LitBook>();
			cbi.setCoComboBox(view.getComboBox());
			cbi.setColumnDescs(ColumnDesc.createPropertyOnlyColumnDescs(
					"title", "year", "author.name"));
			final ISingleSelectionListCobean<LitBook> cobean = cbi.initialize();

			// Bind properties of the component bean to the model.
			BoundPropBinding selBpb = new BoundPropBinding();
			selBpb.setModelPath("selectedBook");
			selBpb.setCompProperty("selectedValue");
			model = selBpb.bind(model, cobean);

			BoundPropBinding dataBpb = new BoundPropBinding();
			dataBpb.setModelPath("books");
			dataBpb.setCompProperty("value");
			model = dataBpb.bind(model, cobean);

			// The model is now bound to the component bean and by that to the
			// component itself.
			// From now on only the model is used for interactions.
		}

		// Set all languages to the model -> the combo box will be filled as it
		// is bound.
		model.setBooks(Service.loadAllBooks());

		view.getShowSelectionButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				log.debug("[actionPerformed] " + e);
				JOptionPane.showMessageDialog(view, ""
						+ StringUtils.abbreviate("" + model.getSelectedBook(),
								150));
			}

		});

		view.getSelect0Button().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				log.debug("[actionPerformed] " + e);
				Iterator<LitBook> iterator = model.getBooks().iterator();
				model.setSelectedBook(iterator.next());
			}

		});
		view.getSelect3Button().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				log.debug("[actionPerformed] " + e);
				Iterator<LitBook> iterator = model.getBooks().iterator();
				iterator.next();
				iterator.next();
				model.setSelectedBook(iterator.next());
			}

		});
		view.getSetAllButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				log.debug("[actionPerformed] " + e);
				model.setBooks(Service.loadAllBooks());
			}
		});
		view.getSetRestrictedButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				log.debug("[actionPerformed] " + e);
				model.setBooks(ExamplesUtil.filterBooksByYear(Service
						.loadAllBooks(), 1990, 10000));
			}
		});
	}

	public ILitService getService() {
		return Service;
	}

	public void setService(ILitService litService) {
		this.Service = litService;
	}

	public ComboBoxShowcaseModel getModel() {
		return model;
	}

	public void setModel(ComboBoxShowcaseModel model) {
		this.model = model;
	}

}
